dojo.require("dojo.parser");
dojo.require("dojox.image.SlideShow");
dojo.require("dojo.data.ItemFileReadStore"); 
	
dojo.addOnLoad(function(){
	
    dojo.parser.parse();
	
    var loadGallery = function(){
	
		var newStore = new dojo.data.ItemFileReadStore({
			url: "index.php?option=com_portfoliozen&task=loadGallery&id=" + dojo.attr('zenslideWrapper', "initial") + "&format=raw"
		});
	
		dijit.byId('slideshow').setDataStore(newStore, {query: {}, count: 20}, {imageLargeAttr: "large"});
		
	}
    
    var loadInfo = function(gallery){

	var metaControls = function(){
	    var that = {};
	    that.content = '';
	    that.buildNav = function(){
		var ul = dojo.byId("imagesNav");
		var slide = dijit.byId("slideshow");
		total = this.content.items.length;
		if(dojo.attr(dojo.byId('imagesNav'), 'reverse') == 'true'){
		    that.buildReverse(ul, slide, total, this);
		} else {
		    that.buildNormal(ul, slide, total, this);	
		}
	    }

	    that.buildNormal = function(ul, slide, total, obj){
		for(var i = 1; i <= total; i++){
		    that.buildListItem(ul, slide, i);
		}
		dojo.addClass(dojo.byId("imagesNav").firstChild, "activeImage");
	    }

	    that.buildReverse = function(ul, slide, total, obj){
		for(var i = total; i >= 1; i--){
		    that.buildListItem(ul, slide, i);
		}
		dojo.addClass(dojo.byId("imagesNav").lastChild, "activeImage");
	    }

	    that.buildListItem = function(ul, slide, i){
		var li = document.createElement("li");
		li.innerHTML = i;
		li.number = i - 1;
		dojo.connect(li, "onclick", function(evt){
		    slide.showImage(li.number);
		    that.updateCaption(li.number);
		    that.stripClass(ul, "activeImage");
		    dojo.addClass(li, "activeImage");
		});
		ul.appendChild(li);
	    }

	    that.updateCaption = function(index){
		dojo.byId("imageCaption").innerHTML = this.content.items[index].content;
	    }

	    that.reset = function(){
		that.content = '';
		that.clearList(dojo.byId("imagesNav"));
		that.stripClass(dojo.byId("galleryMainNav"), "activeCategory"); 	
		dojo.addClass(dojo.byId("gallery" + gallery), "activeCategory");
		dojo.byId('imageCaption').innerHTML = '';
	    }

	    that.clearList = function(ul){
		while (ul.hasChildNodes()) {
		    ul.removeChild(ul.lastChild);
		}
	    }

	    that.stripClass = function(ul, classname){
		var kids = ul.getElementsByTagName("li");
		dojo.forEach(kids, function(obj){
		    dojo.removeClass(obj, classname);
		});
	    }

	    return that;
	}
	
	dojo.xhrGet({
	    url : "index.php?option=com_portfoliozen&task=loadGallery&id=" + gallery + "&type=captions&format=raw",
	    load : function(data) {
		var content = dojo.fromJson(data);
		var meta = metaControls();
		meta.reset();
		if(content.items.length !== 0){
		    meta.content = content;
		    meta.buildNav();
		    dojo.byId("imageCaption").innerHTML = content.items[0].content;
		}
	    }
	});
    }
    
    var initializeTopNav = function(){
	
	var load = function(num){
	    var slide = dijit.byId("slideshow");
	    var newStore = new dojo.data.ItemFileReadStore({
		url: "index.php?option=com_portfoliozen&task=loadGallery&id=" + num + "&format=raw"
	    });
	    slide.setDataStore(newStore, { query : {}, count : 20}, { imageLargeAttr : "large"});
	    loadInfo(num);
	}

	var links = dojo.query("#galleryMainNav li a");
	dojo.forEach(links, function(obj){
	    dojo.connect(obj, "onclick", function(){
		var nodeId = this.parentNode.id;
		nodeId = nodeId.substring(7);
		load(nodeId);
	    });
	});
    }

    loadGallery();
    loadInfo(dojo.attr('zenslideWrapper', 'initial'));
    initializeTopNav();
    
});